package com.example.cdctestsystem.managecontroller;

import com.example.cdctestsystem.pojo.Category;
import com.example.cdctestsystem.pojo.Config;
import com.example.cdctestsystem.pojo.Question;
import com.example.cdctestsystem.pojo.QuestionType;
import com.example.cdctestsystem.service.CategoryService;
import com.example.cdctestsystem.service.QuestionTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/manage/knowledge-structure")
public class KnowledgeStructureController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private QuestionTypeService questionTypeService;

    //转发到添加类别页面
    @RequestMapping("category-add")
    public void categoryAdd(){}

    //带着共享数据，回到类别管理页面
    @RequestMapping("category-list")
    public ModelAndView categoryList(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("categoryList",categoryService.findAll());
        modelAndView.addObject("categoryCount",categoryService.getCategory());
        modelAndView.setViewName("/manage/knowledge-structure/category-list");
        return modelAndView;
    }

    //添加问题的类别
    @RequestMapping("addCategory")
    @ResponseBody
    public void addCategory(Category category){
        categoryService.insert(category);
    }

    //问题类别的禁用和解封
    @RequestMapping("deleteCategory")
    @ResponseBody
    public void deleteCategory(Integer id){
        System.out.println(id);
        categoryService.updateCategory(id);
    }

    //转发到问题类型管理页面
    @RequestMapping("question-type-list")
    @ModelAttribute("questionTypeList")
    public List<QuestionType> questionTypeList(){
        return questionTypeService.findAll();
    }

    //添加节点  添加问题类型
    @RequestMapping("addNode")
    @ResponseBody
    public void addNode(QuestionType questionType){
        questionTypeService.insert(questionType);
    }

    //删除节点   删除问题类型
    @RequestMapping("deleteNode")
    @ResponseBody
    public void deleteNode(Integer id){
        questionTypeService.delete(id);
    }

    //修改节点信息
    @RequestMapping("renameNode")
    @ResponseBody
    public void renameNode(Integer id,String name){
        questionTypeService.updateName(id,name);
    }

    //转发到问题排序页面
    @RequestMapping("type-sort-list")
    public void typeList(){ }

    //转发到问题类型排序修改的页面
    @RequestMapping("type-sort-list-page")
    public ModelAndView questionTypeSortList(String name,Integer pageNum, HttpServletRequest request){
       ModelAndView modelAndView=new ModelAndView();
        if(pageNum==null) pageNum=1;
        Integer pageSize=0;
        ServletContext servletContext =request.getSession().getServletContext();
        List<Config> configList=(List<Config>)servletContext.getAttribute("configs");
        for (Config config:configList
             ) {
            if("manage_question_type_count".equals(config.getKey())){
                pageSize=Integer.parseInt(config.getValue());
            }
        }
        modelAndView.addObject("pageInfo", questionTypeService.findAllQuestionTypes(name,pageNum,pageSize));
        modelAndView.addObject("name",name);
        modelAndView.setViewName("/manage/knowledge-structure/type-sort-data-list");
        return modelAndView;
    }

    //修改问题类型排序
    @RequestMapping("update-sort")
    @ResponseBody
    public void updateSort(QuestionType questionType){
        questionTypeService.update(questionType);
    }

}